package com.employeemanage.controller;

import com.employeemanage.domain.resp.PageResult;
import com.employeemanage.domain.resp.Result;
import com.employeemanage.domain.bean.TblSupplier;
import com.employeemanage.domain.vo.SupplierAddVo;
import com.employeemanage.domain.vo.SupplierListVo;
import com.employeemanage.domain.vo.SupplierUpdateVo;
import com.employeemanage.service.TblSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 供应商表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/product/supplier")
public class TblSupplierController {

    @Autowired
    private TblSupplierService supplierService;

    /**
     * 获取供应商列表
     * @param supplierListVo 查询条件
     * @return 供应商列表
     */
    @PostMapping("/list")
    public Result<PageResult<TblSupplier>> getSupplierList(@RequestBody(required = false) SupplierListVo supplierListVo) {
        try {
            if (supplierListVo == null) {
                supplierListVo = new SupplierListVo();
            }
            PageResult<TblSupplier> result = supplierService.getSupplierList(supplierListVo);
            return Result.success(result);
        } catch (Exception e) {
            return Result.fail("获取供应商列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增供应商
     * @param supplierAddVo 供应商信息
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Map<String, Object>> addSupplier(@RequestBody SupplierAddVo supplierAddVo) {
        try {
            Long supplierId = supplierService.addSupplier(supplierAddVo);
            Map<String, Object> data = new HashMap<>();
            data.put("id", supplierId);
            return Result.success(data);
        } catch (Exception e) {
            return Result.fail("新增供应商失败：" + e.getMessage());
        }
    }

    /**
     * 更新供应商信息
     * @param supplierUpdateVo 供应商信息
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<String> updateSupplier(@RequestBody SupplierUpdateVo supplierUpdateVo) {
        try {
            boolean success = supplierService.updateSupplier(supplierUpdateVo);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.fail("供应商不存在");
            }
        } catch (Exception e) {
            return Result.fail("更新供应商信息失败：" + e.getMessage());
        }
    }

    /**
     * 删除供应商
     * @param request 包含供应商ID的请求
     * @return 删除结果
     */
    @PostMapping("/delete")
    public Result<String> deleteSupplier(@RequestBody Map<String, Long> request) {
        try {
            Long id = request.get("id");
            if (id == null) {
                return Result.fail("供应商ID不能为空");
            }
            
            boolean success = supplierService.deleteSupplier(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.fail("供应商不存在");
            }
        } catch (Exception e) {
            return Result.fail("删除供应商失败：" + e.getMessage());
        }
    }
}
